﻿using MOBAServer.Room;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace MOBAServer.Cache
{
    public class MatchCache:RoomCacheBase<MatchRoom>
    {
        /// <summary>
        /// 玩家进入匹配队列
        /// </summary>
        /// <param name="playerId"></param>
        /// <returns>房间是否满了</returns>
        public MatchRoom StartMatch(MobaClient client,int playerId)
        {
            MatchRoom room = null;
            foreach (var item in idRoomDic.Values)
            {
                if(!item.IsFull)
                {
                    room = item;
                    room.Enter(client,playerId);
                    playerRoomDic.TryAdd(playerId,room.id);
                    return room;
                }
            }
            //有重用的房间
            if(roomQue.Count>0)
            {
                roomQue.TryDequeue(out room);
                //添加映射关系
                idRoomDic.TryAdd(room.id,room);
                playerRoomDic.TryAdd(playerId,room.id);
                //添加玩家进入房间
                room.Enter(client,playerId);
                return room;
            }
            else
            {
                room = new MatchRoom(id,2);
                id++;
                idRoomDic.TryAdd(room.id, room);
                playerRoomDic.TryAdd(playerId, room.id);
                room.Enter(client, playerId);
                return room;
            }
            
        }
        /// <summary>
        /// 玩家离开匹配队列
        /// </summary>
        /// <param name="client"></param>
        /// <param name="playerId"></param>
        /// <returns>离开是否成功</returns>
        public bool StopMatch(MobaClient client,int playerId)
        {
            int temp;
            //安全检测
            if (!playerRoomDic.TryRemove(playerId,out temp))
                return false;
            int roomId = playerRoomDic[playerId];
            MatchRoom room;
            //检测 防止多线程造成不必要的错误
            if (!idRoomDic.TryGetValue(roomId, out room))
                return false;
            room.LeaveRoom(client,playerId);
            if(room.IsEmpty)
            {
                //移除映射关系
                idRoomDic.TryRemove(roomId,out room);
                //移除定时任务
                if(!room.GUID.Equals(new Guid()))
                {
                    room.timer.RemoveAction(room.GUID);
                }
                roomQue.Enqueue(room);
            }
            return true;
        }
        /// <summary>
        /// 摧毁指定的房间
        /// </summary>
        /// <param name="room"></param>
        public void DestroyRoom(MatchRoom room)
        {
            int temp;
            //移除玩家ID和房间ID的映射
            foreach (var item in room.team1IdList)
            {
                playerRoomDic.TryRemove(item,out temp);
            }
            foreach (var item in room.team2IdList)
            {
                playerRoomDic.TryRemove(item, out temp);
            }
            //移除房间ID和房间的映射
            idRoomDic.TryRemove(room.id,out room);
            //清空房间信息
            room.team1IdList.Clear();
            room.team2IdList.Clear();
            room.clientList.Clear();
            //入队列
            roomQue.Enqueue(room);
        }
        /// <summary>
        /// 玩家下线
        /// </summary>
        /// <param name="client"></param>
        public void Offline(MobaClient client,int playerId)
        {
            StopMatch(client,playerId);
        }
    }
}
